package org.jeecg.modules.quartz.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.tz.entity.TzNetworkFlow;
import org.jeecg.modules.tz.service.ITzNetworkFlowService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 示例带参定时任务
 * 
 * @Author Scott
 */
@Slf4j
public class CleanWorkFlowJob implements Job {
	@Autowired
	private ITzNetworkFlowService tzNetworkFlowService;
	@Autowired
	private ISysAnnouncementService sysAnnouncementService;

	/**
	 * 若参数变量名修改 QuartzJobController中也需对应修改
	 */
	private String parameter;

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info(" Job Execution key："+jobExecutionContext.getJobDetail().getKey());
		Integer integer = Integer.valueOf(this.parameter);//清除几天前的数据
		String beforeNumDay = DateUtils.getBeforeNumDay(integer);
		log.info( String.format("welcome %s! Jeecg-Boot 带参数定时任务 SampleParamJob !   时间:" + DateUtils.now(), this.parameter));
		//清除历史回溯数据
		LambdaQueryWrapper<TzNetworkFlow> query = new LambdaQueryWrapper<TzNetworkFlow>();
		query.le(TzNetworkFlow::getReceiveTime,DateUtils.parseDatetime(beforeNumDay));
		tzNetworkFlowService.remove(query);

		//清楚告警
		LambdaQueryWrapper<SysAnnouncement> query2 = new LambdaQueryWrapper<SysAnnouncement>();
		query2.le(SysAnnouncement::getCreateTime,DateUtils.parseDatetime(beforeNumDay));
		sysAnnouncementService.remove(query2);
	}
}
